package cn.edu.ctbu.tydemo.service.impl;

import cn.edu.ctbu.tydemo.domain.Teacher;
import cn.edu.ctbu.tydemo.repository.TeacherRepository;
import cn.edu.ctbu.tydemo.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherimplService implements TeacherService {

    @Autowired
    private TeacherRepository teacherRepository;

    public List<Teacher> findAll(){
        return teacherRepository.findAll();
    }

    /***
     * 按id进行查询
     * @param id 主键，整数型
     * @return
     */
    public Teacher findById(Integer id){
        return teacherRepository.findById(id).orElse(null);
    }

    /***
     * 按name进行查询，like
     * @param name 姓名，字符型
     * @return
     */
    public List<Teacher> findByName(String name){
        return teacherRepository.findByNameLike(name);
    }

    /***
     * 按名字和密码查询
     * @param name 姓名,字符型
     * @param password 密码，字符型
     * @return
     */
    public List<Teacher> findByNameAndPassword(String name,String password){
        return teacherRepository.findByNameAndPassword(name,password);
    }


    /**
     * 只支持分页的查询
     * @param pageable
     * @return
     */
    public Page<Teacher> findAll(Pageable pageable){

        return teacherRepository.findAll(pageable);
    }

    /**
     * 分页，带过滤的查询
     * @param teacher
     * @param pageable
     * @return
     */
    public Page<Teacher> findAll(Example<Teacher> teacher, Pageable pageable){
        return teacherRepository.findAll(teacher,pageable);
    }
    public Teacher insert(Teacher teacher){
        teacherRepository.save(teacher);
        return teacher;
    }

    public Teacher update(Teacher teacher){
        teacherRepository.save(teacher);
        return teacher;
    }


    public void delete(Teacher teacher){

        teacherRepository.delete(teacher);
    }

    public void delete(Integer id){

        Teacher teacher=new Teacher();
        teacher.setId(id);

        teacherRepository.delete(teacher);
    }
}
